//
//  UICollectionView+Extension.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/3.
//

import UIKit

private let collectionHeaderViewTag: Int = 11101

public extension UICollectionView {

    enum SupplementaryViewKind: String {
        case header = "UICollectionElementKindSectionHeader"
        case footer = "UICollectionElementKindSectionFooter"
    }

    func registerCellClass<T>(_ cellClass: T.Type) where T: ReuseIdentifiable {
        register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier)
    }
    
    func registerSupplementaryViewClass<T>(_ viewClass: T.Type, ofKind kind: SupplementaryViewKind) where T: ReuseIdentifiable {
        register(T.self, forSupplementaryViewOfKind: kind.rawValue, withReuseIdentifier: T.reuseIdentifier)
    }
    
    func dequeueReusableCell<T>(_ cellClass: T.Type, for indexPath: IndexPath) -> T where T: ReuseIdentifiable {
        return dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T
    }

    func dequeueReusableSupplementaryView<T>(_ viewClass: T.Type, ofKind kind: SupplementaryViewKind, for indexPath: IndexPath) -> T where T: ReuseIdentifiable {
        return dequeueReusableSupplementaryView(ofKind: kind.rawValue, withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T
    }
    
    /// 像`UITableView.tableHeaderView`一样给`UICollectionView`设置头视图
    /// `collectionHeaderView`需设置高度
    var collectionHeaderView: UIView? {
        set {
            // 移除旧的
            if let view = viewWithTag(collectionHeaderViewTag) {
                view.removeFromSuperview()
                self.removeConstraints(view.constraints)
                var contentInset = self.contentInset
                contentInset.top -= view.frame.size.height
                self.contentInset = contentInset
                var scrollIndicatorInsets = self.scrollIndicatorInsets
                scrollIndicatorInsets.top -= view.frame.size.height
                self.scrollIndicatorInsets = scrollIndicatorInsets
            }
            
            guard let view = newValue else {
                return
            }
            view.tag = collectionHeaderViewTag
            // 添加新的
            let viewHeight = view.frame.size.height
            var contentInset = self.contentInset
            contentInset.top += viewHeight
            self.contentInset = contentInset
            var scrollIndicatorInsets = self.scrollIndicatorInsets
            scrollIndicatorInsets.top += viewHeight
            self.scrollIndicatorInsets = scrollIndicatorInsets
//            view.frame = CGRect(x: 0, y: -viewHeight, width: self.frame.size.width, height: viewHeight)
            addSubview(view)
            view.translatesAutoresizingMaskIntoConstraints = false
            addConstraints(
                [
                    "H:|[header]|",
                    "V:|-top-[header(==viewHeight)]"
                ].flatMap {
                    NSLayoutConstraint.constraints(withVisualFormat: $0, options: [], metrics: ["viewHeight": viewHeight, "top": -viewHeight], views: ["header": view])
                })
            view.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        }
        get {
            return viewWithTag(collectionHeaderViewTag)
        }
    }
}
